package com.hollycrm.ahunicom.sheet.business.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hollycrm.ahunicom.sheet.AhBeanResource;
import com.hollycrm.ahunicom.sheet.business.BusinessSupportManagerDefine;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.business.web.AbstractCommitAction;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

public class SpRefundmentAction extends AbstractCommitAction {
	public ActionForward execDefault(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String sheetFlowNO = (String) request.getParameter("sheetFlowNO");
		String mobileTeleNo = (String) request.getParameter("mobileTeleNo");
		String flowID = (String) request.getParameter("flowID");
		String operateType = (String) request.getParameter("operateType");

		request.setAttribute("sheetFlowNO", sheetFlowNO);
		request.setAttribute("mobileTeleNo", mobileTeleNo);
		request.setAttribute("flowID", flowID);
		request.setAttribute("operateType", operateType);

		return mapping.findForward("addRefundment");
	}
	
	public ActionForward execModifyForm(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		initRequest(request);

		return mapping.findForward("modifyRefundment");
	}
	
	public ActionForward execDeleteForm(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		initRequest(request);

		return mapping.findForward("deleteRefundment");
	}

	public ActionForward execList(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SpRefundmentForm form = (SpRefundmentForm) actionForm;
		String sheetFlowNO = request.getParameter("sheetFlowNO");
		BusinessSupportManagerDefine bsm = getBusinessSupportManagerDefine((SpRefundmentForm) actionForm);
		List spRefundments = bsm.listSpRefundment(sheetFlowNO);
		request.setAttribute("spRefundments", spRefundments);
		return mapping.findForward("list");
	}
	
	public void initRequest(HttpServletRequest request) throws Exception {
		
		String spRefundmentNo = (String) request.getParameter("spRefundmentNo");
		String flowID = (String) request.getParameter("flowID");
		String operateType = (String) request.getParameter("operateType");
		String sheetFlowNO = (String) request.getParameter("sheetFlowNO");
		String mobileTeleNo = (String) request.getParameter("mobileTeleNo");
		String spNO = (String) request.getParameter("spNO");
		String compensation = (String) request.getParameter("compensation");
		String compensateCause = (String) request.getParameter("compensateCause");

		request.setAttribute("flowID", flowID);
		request.setAttribute("operateType", operateType);
		request.setAttribute("spRefundmentNo",spRefundmentNo);
		request.setAttribute("sheetFlowNO",sheetFlowNO);
		request.setAttribute("mobileTeleNo",mobileTeleNo);
		request.setAttribute("spNO",spNO);
		request.setAttribute("compensation",compensation);
		request.setAttribute("compensateCause",compensateCause);
		
	}

	/**
	 * getBusinessSupportManager
	 * 
	 * @param form
	 *            MainSheetForm
	 * @return BusinessSupportManager
	 */
	private BusinessSupportManagerDefine getBusinessSupportManagerDefine(
			SpRefundmentForm form) {
		return (BusinessSupportManagerDefine) getBean(AhBeanResource.BUSINESSSUPPORT_MANAGER_DEFINE);
	}

	public String setupCommitContext(ActionForm actionForm,
			HttpServletRequest request, DoOperateContext doc) throws Exception {
		AuthToken authToken = (AuthToken) request.getSession().getAttribute(
				AuthToken.BEAN_ID);
		SpRefundmentForm form = (SpRefundmentForm) actionForm;
		String modifyFlag = request.getParameter("modifyFlag");
		if("1".equals(modifyFlag)) {
			doc.addInputAttr("spRefundmentNo", request.getParameter("spRefundmentNo"));
			doc.addInputAttr("sheetFlowNO", request.getParameter("sheetFlowNO"));
			doc.addInputAttr("mobileTeleNo", request.getParameter("mobileTeleNo"));
			doc.addInputAttr("spNO", request.getParameter("spNO"));
			doc.addInputAttr("compensation", request.getParameter("compensation"));
			doc.addInputAttr("compensateCause", request.getParameter("compensateCause"));
		} else if("2".equals(modifyFlag)) {
			doc.addInputAttr("spRefundmentNo", request.getParameter("spRefundmentNo"));
		} else {
			doc.addInputAttr("sheetFlowNO", form.getSheetFlowNO());
			doc.addInputAttr("mobileTeleNo", form.getSpRefundment()
					.getMobileTeleNo());
			doc.addInputAttr("spNO", form.getSpRefundment().getSpNo());
			doc.addInputAttr("compensation", form.getSpRefundment()
					.getCompensation());
			doc.addInputAttr("compensateCause", form.getSpRefundment()
					.getCompensateCause());
		}
		doc.addInputAttr("flowID", request.getParameter("flowID"));
		doc.addInputAttr("operateType", request.getParameter("operateType"));
		return null;
	}

}
